package com.hmall.user.controller;  // 修正了包名中的拼写错误 contorller -> controller

import com.hmall.user.domain.dto.LoginFormDTO;
import com.hmall.user.domain.vo.UserLoginVO;
import com.hmall.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Tag(name = "用户相关接口", description = "提供用户登录、余额管理等用户相关操作")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @Operation(
            summary = "用户登录接口",
            description = "用户通过账号密码登录系统，成功后返回令牌和用户基本信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "登录成功",
                            content = @Content(schema = @Schema(implementation = UserLoginVO.class))),
                    @ApiResponse(responseCode = "400", description = "请求参数错误"),
                    @ApiResponse(responseCode = "401", description = "账号或密码错误")
            }
    )
    @PostMapping("login")
    public UserLoginVO login(
            @Parameter(description = "登录表单数据，包含用户名和密码", required = true,
                    schema = @Schema(implementation = LoginFormDTO.class))
            @RequestBody @Validated LoginFormDTO loginFormDTO){
        return userService.login(loginFormDTO);
    }

    @Operation(
            summary = "扣减余额",
            description = "根据支付密码验证后扣减用户相应金额，用于支付等场景",
            responses = {
                    @ApiResponse(responseCode = "200", description = "扣减成功"),
                    @ApiResponse(responseCode = "400", description = "参数错误或余额不足"),
                    @ApiResponse(responseCode = "401", description = "支付密码错误")
            }
    )
    @Parameters({
            @Parameter(name = "pw", description = "支付密码", required = true, in = ParameterIn.QUERY,
                    example = "123456"),
            @Parameter(name = "amount", description = "支付金额，单位：分", required = true, in = ParameterIn.QUERY,
                    example = "100", schema = @Schema(type = "integer", minimum = "1"))
    })
    @PutMapping("/money/deduct")
    public void deductMoney(
            @RequestParam("pw") String pw,
            @RequestParam("amount") Integer amount){
        userService.deductMoney(pw, amount);
    }
}
